package com.example.smartstart.controller;

import com.example.smartstart.entity.MapPoint;
import com.example.smartstart.entity.MapRecord;
import com.example.smartstart.entity.dto.MapRecordDTO;
import com.example.smartstart.model.MyResponseData;
import com.example.smartstart.model.Result;
import com.example.smartstart.service.IMapRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dlx
 * @since 2024-07-22
 */
@RestController
@RequestMapping("mapRecord/")
public class MapRecordController {

    @Autowired
    private IMapRecordService iMapRecordService;

    // 分页查询所有记录
    @GetMapping("listByPage")
    public MyResponseData listByPage(MapRecordDTO mapRecordDTO){
        MyResponseData data = new MyResponseData();
        data.setErrorCode(0);
        data.setMessage("success");
        int row = iMapRecordService.queryTotal(mapRecordDTO);
        System.out.println(row);
        data.setArg1(iMapRecordService.queryTotal(mapRecordDTO));
        data.setList(iMapRecordService.listByPage(mapRecordDTO));
        return data;
    }

    // 查询所有记录
    @GetMapping("getAllRecord")
    public Result getAllRecord(MapRecordDTO mapRecordDTO){
        List<MapRecordDTO> res = iMapRecordService.getAllRecord(mapRecordDTO);
        return Result.success(res);
    }

    // 查询所有记录人数
    @GetMapping("getAllRecordUserCount")
    public Result getAllRecordUserCount(MapRecordDTO mapRecordDTO){
        List<MapRecordDTO> res = iMapRecordService.getAllRecordUserCount(mapRecordDTO);
        return Result.success(res);
    }

    // 增加记录
    @PostMapping("addRecord")
    public Result addRecord(@RequestBody MapRecord mapRecord){
        mapRecord.setCreateTime(LocalDateTime.now());
        Boolean result = iMapRecordService.save(mapRecord);
        if (result){
            return Result.success();
        }else {
            return Result.error("操作失败");
        }
    }
}
